using Senparc.Weixin.Entities;
using Senparc.Weixin.HttpUtility;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using Senparc.Weixin.MP.CommonAPIs;
using System;
namespace Senparc.Weixin.MP.AdvancedAPIs
{
	public static class OAuthApi
	{
		public static string GetAuthorizeUrl(string appId, string redirectUrl, string state, OAuthScope scope, string responseType = "code")
		{
			return string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type={2}&scope={3}&state={4}#wechat_redirect", new object[]
			{
				appId,
				redirectUrl.UrlEncode(),
				responseType,
				scope,
				state
			});
		}
		public static OAuthAccessTokenResult GetAccessToken(string appId, string secret, string code, string grantType = "authorization_code")
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}", new object[]
			{
				appId,
				secret,
				code,
				grantType
			});
			return CommonJsonSend.Send<OAuthAccessTokenResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000, false);
		}
		public static OAuthAccessTokenResult RefreshToken(string appId, string refreshToken, string grantType = "refresh_token")
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={0}&grant_type={1}&refresh_token={2}", appId, grantType, refreshToken);
			return CommonJsonSend.Send<OAuthAccessTokenResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000, false);
		}
		public static OAuthUserInfo GetUserInfo(string accessToken, string openId, Language lang = Language.zh_CN)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang={2}", accessToken, openId, lang);
			return CommonJsonSend.Send<OAuthUserInfo>(null, urlFormat, null, CommonJsonSendType.GET, 10000, false);
		}
		public static WxJsonResult Auth(string accessToken, string openId)
		{
			string urlFormat = string.Format("https://api.weixin.qq.com/sns/auth?access_token={0}&openid={1}", accessToken, openId);
			return CommonJsonSend.Send<WxJsonResult>(null, urlFormat, null, CommonJsonSendType.GET, 10000, false);
		}
	}
}
